import 'package:beautiful_demo/common/dsliver.dart';
import 'package:beautiful_demo/common/manager/user.dart';
import 'package:beautiful_demo/common/res.dart';
import 'package:beautiful_demo/mvc/home/child/widget.dart';
import 'package:drive/drive.dart';

import 'controller.dart';
import 'package:flutter/material.dart';
import 'widget.dart';
import 'package:flutter_mvc/flutter_mvc.dart';

///description:聊天室
class RoomPage extends BasePage<ChatRoomController> {

  List<Widget> _buildSliverList() {
    final list = <Widget>[];

    //是否有下一页
    if(c.isMore) {
      list.add( SliverToBoxAdapter(child: MoreWidget()));
    }
    for (var groupItem in c.dataList) {
      list.add(SliverToBoxAdapter(child: TimeWidget(groupItem.time)));

      list.add(DSliverList(builder:(ctx, index) {
        final item = groupItem.list[index];
        if (item.userId == UserManager.instance.data.id) {
          return MeItem(item);
        } else {
          return YouItem(item);
        }
      }, childCount: groupItem.list.length));
    }


    return list;
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: GestureDetector(
        onTap: () {
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Column(
          children: [
            Stateful(
                bind: ()=>[c.y],
                builder: (context) {
                  return SizedBox(height: c.y,);
                }
            ),
            Expanded(
              child: Container(
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: resBorderRadius(topRight: 22.dp, topLeft: 22.dp)),
                child: Column(
                  children: [
                    InfoWidget(),
                    Expanded(
                        child: Container(
                      decoration: BoxDecoration(
                          color: const Color(0xfff1f3f6),
                          borderRadius:
                              resBorderRadius(topLeft: 22.dp, bottomLeft: 22.dp)),
                      child: Column(
                        children: [
                          Expanded(
                              child: Stack(
                            children: [
                              Positioned.fill(
                                  child: CustomScrollView(
                                    controller: c.scrollController,
                                    physics: const BouncingScrollPhysics(),
                                slivers: [
                                  // CompositeSliverScroll(
                                  //   children: _buildList(),
                                  // ),


                                  for(var item in  _buildSliverList())
                                    item,

                                  SliverToBoxAdapter(child:  Container(height: 75.dp,),)
                                ],
                              )),
                              Positioned(
                                  left: 0,
                                  right: 0,
                                  bottom: 0,
                                  child: SendMessageWidget()),
                            ],
                          )),
                          Stateful(
                              bind: () => [c.keyboardState, c.bottomHeight],
                              builder: (context) {
                                return Container(
                                    height: c.bottomHeight,
                                    color: Colors.white,
                                    child: _buildItem());
                              })
                        ],
                      ),
                    ))
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildItem() {
    int index = 0;
    if (c.keyboardState == KeyboardSpecState.Emoji) {
      index = 1;
      // return EmojiWidget();
    }
    // return Container();
    return IndexedStack(
      index: index,
      children: [Container(), EmojiWidget()],
    );
  }
}
